﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using WpfApplication1.Class.Bean;
using WpfApplication1.GestionBug;

namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class BugWindow : Window
    {
        public BugWindow()
        {
            InitializeComponent();
            this.Title += " | " + ClassCurrentUserSingleton.Instance.Nom
                               + " "
                               + ClassCurrentUserSingleton.Instance.Prenom
                               + ", "
                               + ClassCurrentUserSingleton.Instance.Role;

            List<SearchField> listSF = new List<SearchField>();
            List<ClassBug> listBug = ClassBug.Search(listSF);

            Binding bind = new Binding();
            bind.Source = listBug;
            this.listView.DisplayMemberPath = "Nom";
            this.listView.SetBinding(DataContextProperty, bind);
            
        }

        private void AddBug(object sender, RoutedEventArgs e)
        {
            if (ClassCurrentUserSingleton.Instance.Role == ClassPersonne.ROLE_DEVELOPPEUR)
                MessageBox.Show("Seul les testeurs et chefs de projet peuvent signaler un bug !");
            else
            {
                AddBugWindow addBugWindow = new AddBugWindow(this.listView);
                addBugWindow.Show();
            }
        }


        private void DisplayGestionBug(object sender, MouseButtonEventArgs e)
        {
            PageGestion page = new PageGestion((ClassBug)this.listView.SelectedItem);
            page.Show();
            
        }

        private void DisplayInfoBug(object sender, SelectionChangedEventArgs e)
        {
            if (this.listView.SelectedItem != null)
            {
                ClassBug bug = (ClassBug)this.listView.SelectedItem;

                this.nameLabel.Visibility = Visibility.Visible;
                this.nameBlock.Text = bug.Nom;

                this.emissionLabel.Visibility = Visibility.Visible;
                this.emissionBlock.Text = bug.DateEmission.ToShortDateString();

                this.resolutionLabel.Visibility = Visibility.Visible;
                if (bug.Etat.Equals(ClassBug.ETAT_OUVERT))
                    this.resolutionBlock.Text = "Aucune date de résolution";
                else
                    this.resolutionBlock.Text = bug.DateResolution.ToShortDateString();

                this.fermetureLabel.Visibility = Visibility.Visible;
                if (!bug.Etat.Equals(ClassBug.ETAT_FERME))
                    this.fermetureBlock.Text = "Aucune date de fermeture";
                else
                    this.fermetureBlock.Text = bug.DateFermeture.ToShortDateString();

                //etatBlock, typeBlock, prioriteBlock, emmeteurBlock, responsableBlock; envrionnementLabel

                this.etatLabel.Visibility = Visibility.Visible;
                this.etatBlock.Text = bug.Etat;

                this.typeLabel.Visibility = Visibility.Visible;
                this.typeBlock.Text = bug.Type;

                this.prioriteLabel.Visibility = Visibility.Visible;
                this.prioriteBlock.Text = bug.Priorite;

                List<SearchField> list1 = new List<SearchField>();
                list1.Add(new SearchField("Id", bug.IdEmmeteur, SearchField.EGAL_A));
                List<ClassPersonne> listEmmeteur = ClassPersonne.Search(list1);
                this.emmeteurLabel.Visibility = Visibility.Visible;
                this.emmeteurBlock.Text = listEmmeteur.First().Nom;

                List<SearchField> list2 = new List<SearchField>();
                list2.Add(new SearchField("Id", bug.IdProjet, SearchField.EGAL_A));
                List<ClassProjet> listProjet = ClassProjet.Search(list2);
                this.projetLabel.Visibility = Visibility.Visible;
                this.projetBlock.Text = listProjet.First().NomProjet;

                List<SearchField> list3 = new List<SearchField>();
                list3.Add(new SearchField("Id", bug.IdResponsable, SearchField.EGAL_A));
                List<ClassPersonne> listResponsable = ClassPersonne.Search(list3);
                this.responsableLabel.Visibility = Visibility.Visible;
                this.responsableBlock.Text = listResponsable.First().Nom;

                this.envrionnementLabel.Visibility = Visibility.Visible;
                this.environnementBlock.Text = bug.Environnement;
            }
        }

        private void Filter(object sender, RoutedEventArgs e)
        {
            FilterWindow filter = new FilterWindow(this.listView);
            filter.Show();
        }

        private void CancelFilter(object sender, RoutedEventArgs e)
        {
            List<SearchField> listSF = new List<SearchField>();
            List<ClassBug> listBug = ClassBug.Search(listSF);

            Binding bind = new Binding();
            bind.Source = listBug;
            this.listView.DisplayMemberPath = "Nom";
            this.listView.SetBinding(DataContextProperty, bind);
        }
    }
}
